package com.fengweijie.service.impl;

import com.fengweijie.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Service
@Slf4j
public class FileServiceImpl implements FileService {
    @Override
    public String uploadFile(MultipartFile file) {
        if (file.isEmpty()) {
            return null;
        }

        // 获取文件名并构建存储路径
        String fileName = file.getOriginalFilename();
        File dest = new File(System.getProperty("user.dir")+"/assets", fileName);

        try {
            // 将文件内容写入到目标文件
            file.transferTo(dest);
            System.out.println("文件上传成功: " + fileName);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件上传失败: " + e.getMessage());
        }
        log.info("上传成功");
        return "assets/"+fileName;
    }

    @Override
    public void deleteFile(List<String> fileNameList) throws Exception {

    }
}
